package com.gopay.bis.cmbc.ebank.operation;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.hitrust.B2B.CMBC.common.MessageTagName;
import com.hitrust.B2B.CMBC.request.QueryOrderRequest;
import com.hitrust.B2B.CMBC.response.GenericResponse;
import com.hitrust.B2B.CMBC.util.DateUtil;
import com.hitrust.B2B.CMBC.util.XMLDocument;

public class CmbcB2bQueryCerthelper {
	/**
	 * 民生B2B订单查询签名
	 * 
	 * @param message
	 * @return
	 * @throws GopayException 
	 */
	public CmbcMessage getB2bOrder(BaseMessage baseMessage) throws GopayException {
		try {			
			CmbcMessage message = (CmbcMessage) baseMessage;
			BankLogUtils.printB2BQuerySendLog(message, " CMBC B2B query orderid:"+ message.getOrderId());
			String dateTime = DateUtil.getCurrentTime();
			// 创建订单查询交易请求对象
			QueryOrderRequest queryOrder = new QueryOrderRequest();
			/** 商户交易序号 ，为确保其唯一性后缀为当前时间 */
			queryOrder.setMerchantTrnxNo(message.getMerchantTrnxNo() + dateTime);
			/** 交易请求代码 */
			queryOrder.setTrnxCode(message.getTrnxCode());
			/** 要查询的订单号 */
			queryOrder.setOrderNo(message.getOrderId());
			/** 创建订单查询应答结果对象 */
			GenericResponse res = queryOrder.postRequest();
			/** 订单查询交易结果处理代码 */
			String code = res.getCode();
			/** 订单查询交易描述信息 */
			String resMessage = res.getMessage();
			/** 订单对象 */
			String order = res.getValue(MessageTagName.ORDER_INFO_TAG);
			/** 订单交易金额 */
			String orderAmt = "0";
			/** 订单状态描述 */
			String orderStatus = " ";
			if (!"".equals(order)) {
				orderAmt = res.getValue(MessageTagName.ORDER_INFO_ORDER_AMT_TAG);
				orderStatus = new XMLDocument(order).getValueNoNull(MessageTagName.ORDER_INFO_STATUS_TAG);
			}
			message.setResCode(code);
			message.setResMessage(resMessage);
			message.setOrderAmt(orderAmt);
			message.setOrderStatus(orderStatus);
			BankLogUtils.printB2BQueryBlackLog(message, message);
			return message;
		}catch(Exception e){
    		BankLogUtils.printB2CQueryExceptionLog(baseMessage, e);
    		throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
    	}
	}
}
